using System.Text.RegularExpressions;
using Headspring.Training.Core.Model;
using Headspring.Training.Infrastructure.Impl;
using NUnit.Framework;
using StructureMap;
using WatiN.Core;

namespace Headspring.Training.FullSystemTests
{
	[TestFixture, Explicit]
	public class EditAssignedWorkOrderTester : BaseTester
	{
		public override void Setup()
		{
			base.Setup();

			var repo = ObjectFactory.FillDependencies<WorkOrderRepository>();
			var employeeRepo = ObjectFactory.FillDependencies<EmployeeRepository>();
			var workOrder = new WorkOrder();

			Employee jeffrey = employeeRepo.GetByUserName("jpalermo");
			workOrder.Status = WorkOrderStatus.Assigned;
			workOrder.Assignee = jeffrey;
			workOrder.Creator = jeffrey;
			workOrder.Number = "PINKO";
			workOrder.Title = "Blarg";
			workOrder.Description = "Here is teh descripshun";

			repo.Save(workOrder);

			LoginAsJeffrey();
		}

		[Test]
		public void Should_Begin_And_Save_Changes()
		{
			NavigateToOrderEdit("PINKO");

			Button saveButton = _browser.Button(Find.ByValue("Begin"));
			TextField titleField = _browser.TextField(new Regex("title"));
			TextField descriptionField = _browser.TextField(new Regex("description"));

			titleField.Value = "test1";
			descriptionField.Value = "test2";

			saveButton.Click();


			StringAssert.Contains("workorder", _browser.Url);
			StringAssert.Contains("You have begun", _browser.Text);
			titleField = _browser.TextField(new Regex("title"));
			descriptionField = _browser.TextField(new Regex("description"));

			Assert.AreEqual("test1", titleField.Value);
			Assert.AreEqual("test2", descriptionField.Value);
		}

		[Test]
		public void Should_only_have_begin_and_reject_available()
		{
			NavigateToOrderEdit("PINKO");

			ButtonCollection buttons = _browser.Buttons;

			Assert.AreEqual(2, buttons.Length);
			Assert.AreEqual("Begin", buttons[0].Text);
			Assert.AreEqual("Reject", buttons[1].Text);
		}


		[Test, Explicit]
		public void Should_Reject_And_Save_Changes()
		{
			NavigateToOrderEdit("PINKO");

			Button saveButton = _browser.Button(Find.ByValue("Reject"));
			TextField titleField = _browser.TextField(new Regex("title"));
			TextField descriptionField = _browser.TextField(new Regex("description"));

			titleField.Value = "test1";
			descriptionField.Value = "test2";

			saveButton.Click();


			StringAssert.Contains("workorder", _browser.Url);
			StringAssert.Contains("You have rejected", _browser.Text);
			titleField = _browser.TextField(new Regex("title"));
			descriptionField = _browser.TextField(new Regex("description"));

			Assert.AreEqual("test1", titleField.Value);
			Assert.AreEqual("test2", descriptionField.Value);
		}
	}
}